package visualcbir;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import visualcbir.util.VSUtils;

/**
 *
 * @author sheila
 */

public class PnlImage extends JPanel implements ActionListener{

    PnlDisplayImage oPnlDisplayImage;
    JButton btnSelectImage;
    Image img = null;
    Principal oPrincipal;

    public PnlImage(Principal poPrincipal){
        super();
        this.oPrincipal = poPrincipal;
        oPnlDisplayImage = new PnlDisplayImage();
        btnSelectImage = new JButton("Select Image");
        btnSelectImage.addActionListener(this);

        this.setLayout(new BorderLayout());
        this.oPnlDisplayImage.setSize(this.getWidth(), this.getWidth());
        this.add(oPnlDisplayImage, BorderLayout.CENTER);
        this.add(btnSelectImage, BorderLayout.SOUTH);        
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource().equals(btnSelectImage)){
         javax.swing.JFileChooser chooser = new JFileChooser();
            int result = chooser.showOpenDialog(this);
            if(result == JFileChooser.APPROVE_OPTION) {
                try {
                    String filename = chooser.getSelectedFile().getAbsolutePath();               

                    if(filename.endsWith(".pgm") || filename.endsWith(".ppm") || filename.endsWith(".jpg") ) {
                        PlanarImage planarImage = JAI.create("fileload",filename);
                        BufferedImage bImage = planarImage.getAsBufferedImage();
                        //ImageFilter colorFilter = new PGMFilter();
                        //Image dst = createImage(new FilteredImageSource(bImage.getSource(),colorFilter));
                        Graphics g = bImage.getGraphics();
                        this.img=VSUtils.getScaledImage(this.getWidth(), bImage);
                        this.oPnlDisplayImage.repaint();
                        this.updateUI();
                        this.oPrincipal.updateQueryImage(filename);
                    }
                    else
                        JOptionPane.showMessageDialog(null, "Deve escolher uma imagem em formato pgm ou ppm.");
                    
                    
                }
                catch(Throwable t){}
            }       
        }
    }
  
    public class PnlDisplayImage extends JPanel{

        public void paint(java.awt.Graphics g) {
            this.setPreferredSize(new Dimension(this.getWidth(), (int) (this.getWidth()*0.98 )));

            if(img == null) {                
                GradientPaint gradient = new GradientPaint(0, 0, Color.BLACK, 175, 175, Color.RED,true);
                ((Graphics2D)g).setPaint(gradient);
                g.fillRect(0, 0, this.getWidth(), this.getHeight());
                VSUtils.resetGraphics(g);
                g.setColor(Color.white);
                g.drawString("No Image", (getWidth()/2)-30, (getHeight()/2)-5);
                VSUtils.resetGraphics(g);
            }
            else{
                int xposition=0, yposition=0;
                if(img.getWidth(null)<this.getWidth())   xposition = (this.getWidth() - img.getWidth(null))/2;
                if(img.getHeight(null)<this.getHeight())   yposition = (this.getHeight() - img.getHeight(null))/2;

                g.drawImage(img,xposition,yposition,null);
                g.setColor(Color.red);
                g.drawRect(xposition, yposition, img.getWidth(this), img.getHeight(this));                
            }             
        }
    }
    
}
